package com.cwt_taxi_xa.protocol.config;

import android.content.Intent;

import com.cwt_taxi_xa.MainApplication;
import com.cwt_taxi_xa.constants.Constants;
import com.cwt_taxi_xa.constants.MyStatus;
import com.cwt_taxi_xa.constants.SPConstants;
import com.cwt_taxi_xa.protocol.Encoder;
import com.cwt_taxi_xa.service.FloatService;
import com.cwt_taxi_xa.util.AppLog;
import com.cwt_taxi_xa.util.SPUtils;

/**
 * 定时拍照
 */
public class PhotographTask {
    private static final String TAG = "PhotographTask";
    private Thread thread;
    //定时拍照:0不开;1全程打开;2重车状态
    private int photographTakeType = 0;
    //定时拍照时间间隔，单位秒
    private int timeInterval = 5;
    //定时拍照分辨率:0：320×240；1：640×480；2：800×600；其他保留
    private int photographQualityType = 0;

    public void setPhotographTakeType(int photographTakeType) {
        this.photographTakeType = photographTakeType;
        if(this.photographTakeType == 1){
            restart();
        }else{
            stopThread();
        }

    }

    public void setTimeInterval(int timeInterval) {
        this.timeInterval = timeInterval;
        restart();
    }



    public void setPhotographQualityType(int photographQualityType) {
        this.photographQualityType = photographQualityType;
        restart();
    }

    public void init(){
        this.photographTakeType = SPUtils.getParamInteger(SPConstants.PHOTOGRAPH_TAKE_TYPE, 0);
        //定时拍照时间间隔，单位秒
        this.timeInterval = SPUtils.getParamInteger(SPConstants.PHOTOGRAPH_INTERVAL_TIME, 5);
        //定时拍照分辨率:0：320×240；1：640×480；2：800×600；其他保留
        this.photographQualityType = SPUtils.getParamInteger(SPConstants.PHOTOGRAPH_QUALITY_TYPE, 0);
        startThread();
    }

    public void restart(){
        stopThread();
        startThread();
    }
    //当连上平台是启动定时抓拍
    public synchronized void startThread(){
        //先屏蔽拍照功能
        if(thread == null && photographTakeType ==1 && Constants.SETTING_PHOTOGRAPH_ENABLE){
            thread = new Thread(runnable);
            thread.start();
            AppLog.d(TAG,"startThread thread.start():");
        }
    }

    public synchronized void stopThread(){
        if(thread != null ){
            AppLog.d(TAG,"stopThread thread.interrupt():");
            thread.interrupt();
            thread = null;
        }
    }

    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            AppLog.d(TAG,"photographTakeType:" + photographTakeType + " MyStatus.online:" + MyStatus.online);
            while (!Thread.currentThread().isInterrupted() && photographTakeType ==1 && MyStatus.online){
                try{
                    Thread.sleep(timeInterval * 1000);
                    //tackPhotograph();
                    AppLog.d(TAG,"tackPhotograph:");
                }catch (Exception e){
                    e.printStackTrace();
                    break;
                }
            }
        }
    };

    private void tackPhotograph(){
        int channelId = 1;//默认会减1,所以不要设置为0
        Intent intent = new Intent(FloatService.action_camera);
        intent.putExtra("type", 2);
        intent.putExtra("seq", Encoder.getSerialId2());
        intent.putExtra("count", 1);
        intent.putExtra("timeInterval", timeInterval);
        intent.putExtra("channelId", channelId);
        switch (photographQualityType) {
            case 0:
                intent.putExtra("w", 320);
                intent.putExtra("h", 240);
                break;
            case 1:
                intent.putExtra("w", 640);
                intent.putExtra("h", 480);
                break;
            case 2:
                intent.putExtra("w", 800);
                intent.putExtra("h", 600);
                break;
            default:
                intent.putExtra("w", 1280);
                intent.putExtra("h", 720);
                break;
        }

        try {
            MainApplication.topActivity.sendBroadcast(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
